#ifndef FILESYSTEMMODEL_H
#define FILESYSTEMMODEL_H

#include <QObject>
#include <QStringList>
#include <QIcon>
#include <QAbstractItemModel>
#include "database/database_manager.h"

class FileSystemModel : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString currentPath READ currentPath WRITE setCurrentPath NOTIFY currentPathChanged)
    Q_PROPERTY(QVariantList drives READ drives NOTIFY drivesChanged)
    Q_PROPERTY(QStringList driveNames READ driveNames NOTIFY drivesChanged)

public:
    explicit FileSystemModel(DatabaseManager *databaseManager = nullptr, QObject *parent = nullptr);
    
    QString currentPath() const;
    void setCurrentPath(const QString &path);
    
    QVariantList drives() const;
    QStringList driveNames() const;
    
    Q_INVOKABLE void refresh();
    Q_INVOKABLE bool isDirectory(const QString &path);
    Q_INVOKABLE QString getFileName(const QString &path);
    Q_INVOKABLE QString getFileSize(const QString &path);
    Q_INVOKABLE QString getFileType(const QString &path);
    Q_INVOKABLE QIcon getFileIcon(const QString &path);
    Q_INVOKABLE QStringList getDirectoryContents(const QString &path, bool includeFiles = true);
    Q_INVOKABLE QStringList getCurrentDirectoryFiles();
    Q_INVOKABLE QStringList getDirectChildren(const QString &parentId);
    Q_INVOKABLE QString getFolderName(const QString &folderId);
    Q_INVOKABLE int getFolderStatus(const QString &folderId);
    Q_INVOKABLE int getFolderType(const QString &folderId);
    Q_INVOKABLE QString getFolderDiskId(const QString &folderId);
    Q_INVOKABLE QString getFileFolderPath(const QString &fileId);
    Q_INVOKABLE int getFileStatus(const QString &fileId);
    Q_INVOKABLE QString getDiskIdByName(const QString &diskName);
    Q_INVOKABLE QString getDiskNameById(const QString &diskId);
    Q_INVOKABLE QVariantList getAllDisks();
    Q_INVOKABLE void setCurrentDisk(const QString &diskId);
    Q_INVOKABLE QString getCurrentDiskId();
    Q_INVOKABLE QString getFileUrl(const QString &fileId);
    Q_INVOKABLE qint64 getLocalFileSize(const QString &fileId);
    Q_INVOKABLE QString getLocalFileChecksum(const QString &fileId);
    Q_INVOKABLE QString getLocalFilePath(const QString &fileId);
    
    // 路径导航相关方法
    Q_INVOKABLE bool canGoUp(const QString &folderId);
    Q_INVOKABLE QString getParentPath(const QString &folderId);

signals:
    void currentPathChanged();
    void drivesChanged();
    void currentDirectoryFilesChanged();
    // 转发文件系统变更（用于触发界面刷新）
    void filesChanged();

private:
    QString m_currentPath;
    QVariantList m_drives;
    QStringList m_driveNames;
    QStringList m_currentDirectoryFiles;
    DatabaseManager *m_databaseManager;
    QString m_currentDiskId;
    
    void updateDrives();
    void updateCurrentDirectoryFiles();
    QStringList getDatabaseDirectoryContents(const QString &path, bool includeFiles = true);
    QStringList getDirectChildrenByParentId(const QString &parentId);
};

#endif // FILESYSTEMMODEL_H
